/*
Copyright (C) 2004-2007  Andy Mutton

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Contact: andy@5263.org
*/
var BACKGROUND_DIV = "screengrabBackgroundDiv";
var DRAW_DIV = "screengrabDrawDiv";
var BOX_DIV = "screengrabBoxDiv";
var SIZER = "sizerDiv";

var originX = null;
var originY = null;
var startDrag = false;
var startSize = false;

function beginBoxDraw(event) {
	var winCon = window._content;
    var boxDiv = winCon.document.getElementById(BOX_DIV);
    if (boxDiv == null) {
        boxDiv = winCon.document.createElement("div");
        boxDiv.setAttribute("id", BOX_DIV);
        boxDiv.setAttribute("class", "boxOverlay");
	    winCon.document.getElementById(BACKGROUND_DIV).appendChild(boxDiv);
    }
    var sizer = winCon.document.getElementById(SIZER);
    if (sizer != null) {
    	sizer.style.display = "none";
    }
    originX = event.clientX;
    originY = event.clientY;
    new Drag.Move(BOX_DIV);
    if (boxDiv.style.display != "inline") {
	    if (boxDiv != null) {
	    	boxDiv.removeAttribute("onmousedown");
	    	boxDiv.removeAttribute("onmousemove");
	    	boxDiv.removeAttribute("onmouseup");
	    }
	
	    boxDiv.style.display = "none";
	    boxDiv.style.left = event.clientX + "px";
	    boxDiv.style.top = event.clientY + "px";
	    boxDiv.setAttribute("onmousedown", "clearBox(event)");
	    boxDiv.style.cursor = "crosshair";
	    winCon.document.addEventListener("mousemove", boxDrawing, true);
	    winCon.document.addEventListener("mouseup", endBoxDraw, true);
    } else {
    	if (!inBox(boxDiv, event)) {
    		if (boxDiv != null) {
		    	boxDiv.removeAttribute("onmousedown");
		    	boxDiv.removeAttribute("onmousemove");
		    	boxDiv.removeAttribute("onmouseup");
		    }
		
		    boxDiv.style.display = "none";
		    boxDiv.style.left = event.clientX + "px";
		    boxDiv.style.top = event.clientY + "px";
		    boxDiv.setAttribute("onmousedown", "clearBox(event)");
		    boxDiv.style.cursor = "crosshair";
		    winCon.document.addEventListener("mousemove", boxDrawing, true);
		    winCon.document.addEventListener("mouseup", endBoxDraw, true);
    	} else {
    		boxDiv.style.cursor = "move";
    	}
    }
}

function boxDrawing(event) {
	var winCon = window._content;
    var boxDiv = winCon.document.getElementById(BOX_DIV);

    var mouseX = event.clientX;
    var mouseY = event.clientY;
    
    var left = mouseX < originX ? mouseX : originX;
    var top = mouseY < originY ? mouseY : originY;

    var width = Math.abs(mouseX - originX);
    var height = Math.abs(mouseY - originY);

    boxDiv.style.display = "none";
    boxDiv.style.left = left + "px";
    boxDiv.style.top = top + "px";

    boxDiv.style.width = width + "px";
    boxDiv.style.height = height + "px";
    
    boxDiv.style.display = "inline";
}

function endBoxDraw(event) {
	var winCon = window._content;
    winCon.document.removeEventListener("mousemove", boxDrawing, true);
    winCon.document.removeEventListener("mouseup", endBoxDraw, true);
    
    var winCon = window._content;
	var boxDiv = winCon.document.getElementById(BOX_DIV);
    if (boxDiv != null) {
    	boxDiv.setAttribute("onmousedown", "beginBoxDrag(event)");
    	boxDiv.setAttribute("onmousemove", "boxDragging(event)");
    	boxDiv.setAttribute("onmouseup", "endBoxDrag(event)");
    }
    var sizer = winCon.document.getElementById(SIZER);
    if (sizer == null) {
    	sizer = winCon.document.createElement("div");
        sizer.setAttribute("id", SIZER);
        sizer.setAttribute("class", "siteshotsizer");
	    boxDiv.appendChild(sizer);
    } else {
    	boxDiv.removeChild(sizer);
    	sizer = winCon.document.createElement("div");
        sizer.setAttribute("id", SIZER);
        sizer.setAttribute("class", "siteshotsizer");
	    boxDiv.appendChild(sizer);
    }
    new Drag.Move(SIZER, {"onDrag": onSizer, "onComplete": onSized});
}

function onSizer(event) {
	var winCon = window._content;
    var boxDiv = winCon.document.getElementById(BOX_DIV);
    var boxLeft = getNumberValueWithoutUnit(boxDiv.style.left);
    var boxTop = getNumberValueWithoutUnit(boxDiv.style.top);
    var sizer = winCon.document.getElementById(SIZER);
    startSize = true;
	if (startSize) {
	
	    var mouseX = getNumberValueWithoutUnit(sizer.style.left)+ 4;
	    var mouseY = getNumberValueWithoutUnit(sizer.style.top)+ 4;
	
	    boxDiv.style.display = "none";
	    boxDiv.style.width = (mouseX) + "px";
	    boxDiv.style.height = (mouseY) + "px";
	
	    boxDiv.style.display = "inline";
	}
}

function onSized(event) {
	startSize = false;
}


function beginBoxDrag(event) {
	startDrag = true;
	var winCon = window._content;
    var boxDiv = winCon.document.getElementById(BOX_DIV);
	originX = event.clientX;
    originY = event.clientY;
}

function boxDragging(event) {
	var winCon = window._content;
    var boxDiv = winCon.document.getElementById(BOX_DIV);
	boxDiv.style.cursor = "move";
	if (startDrag) {
	
	    var mouseX = event.clientX;
	    var mouseY = event.clientY;
	    
	    var left = mouseX - originX; 
	    var top = mouseY - originY; 
	
	    boxDiv.style.display = "none";
	    boxDiv.style.left = (getNumberValueWithoutUnit(boxDiv.style.left) + left) + "px";
	    boxDiv.style.top = (getNumberValueWithoutUnit(boxDiv.style.top) + top) + "px";
	
	    boxDiv.style.display = "inline";
	    
	    originX = event.clientX;
    	originY = event.clientY;
	}
}

function endBoxDrag(event) {
    startDrag = false;
}

function getNumberValueWithoutUnit(value) {
	var stringValue = value;
	var expr = /\D/g;
	stringValue = stringValue.replace(expr,'');
	return parseInt(stringValue);
}

function inBox(box, event) {
	var left = getNumberValueWithoutUnit(box.style.left);
	var top = getNumberValueWithoutUnit(box.style.top);
	var width = getNumberValueWithoutUnit(box.style.width);
	var height = getNumberValueWithoutUnit(box.style.height);
	if (event.clientX < left || event.clientY < top
		|| event.clientX > left + width
		|| event.clientY > top + height) {
		return false;
	} else {
		return true;
	}
}